import tkinter as tk

import windows.WindowsMain as WindowsMain
from Chara import CharaLoader
from windows.SubWindows.CharaView import CharaView
from windows.SubWindows.SubWindow import SubWindow
from windows.SubWindows.SubWindowWidget import WidgetStyleConfig


class CharaInfo(SubWindow):

    def __init__(self, windows: WindowsMain):
        """
        DESCRIPT
            人物图鉴窗口 显示所有的角色头像

        @param windows: WindowsMain 类
        """
        self.titleWSC = WidgetStyleConfig
        self.titleWSC.text = "人物图鉴"
        windows.state="图鉴"

        super().__init__(windows)

    def boxPack(self):
        self.SubWindowWidget.title(WidgetStyleConfig=self.titleWSC, position="center")
        # 标题的设置
        self.frame = self.SubWindowWidget.pack_sub_viewer()
        self.frame['bg'] = '#2B2B2B'

        self.window.win.update()

        self.ShowAlls()
        self.window.win.update()


    def ShowAlls(self):
        global imgs,w,CharaList

        print(self.window.win.winfo_width())
        maxCol = 0
        w, h = self.window.win.winfo_width(), self.window.win.winfo_height()
        print(w)
        maxCol = w // 200
        CharaList = CharaLoader.CharaFactory.get_all_charas()
        print(CharaList)
        count = 0
        i = CharaList
        imgs = [i().get_tkinter_image(resize=(200, 200)) for i in CharaList]
        print(imgs)
        row = 0
        flag_over = False
        a = 0
        while not flag_over:
            for i in range(maxCol):
                if maxCol * row + i + 1 > len(imgs):
                    print(maxCol * row + i + 1, len(imgs))
                    return
                index = maxCol * row + i
                frame = self.frame
                w = self.window
                leb = tk.Label(frame, image=imgs[index], bg='#2B2B2B')

                exec(f'''leb.bind("<Button-1>", lambda e:CharaView(w, CharaList[{a}],thread_open=True))''')
                leb.grid(column=i, row=row)
                leb = None
                a += 1
            row += 1


if __name__ == '__main__':
    ...
